from datetime import datetime
from typing import Optional
from fastapi import APIRouter, Query, HTTPException
from apps.schemas.schemas import CoachCreateRequest, CoachUpdateRequest
from apps.services.coach import (
    get_all_coaches,
    get_all_coaches_for_student,
    get_coach_by_id,
    get_coach_by_user_id,
    create_coach,
    update_coach,
    delete_coach
)

coaches = APIRouter()

@coaches.get("/msg")
async def get_all_coaches_api(
    username: Optional[str] = Query(None, alias="username"),
    search: Optional[str] = Query(None, description="搜索关键字"),
    page: int = Query(1, ge=1),
    page_size: int = Query(10, ge=1, le=100)
):
    """
    获取所有教练列表（管理员端使用）
    """
    try:
        total, coaches_data = await get_all_coaches(username, search, page, page_size)
        return {
            "status": 200,
            "message": "success",
            "data": coaches_data,
            "total": total,
            "page": page,
            "page_size": page_size
        }
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving coaches: {str(e)}"}


@coaches.get("/msg/msg")
async def get_all_coaches_for_student_api(
    search: Optional[str] = Query(None, description="搜索关键字"),
    page: int = Query(1, ge=1, description="页码"),
    page_size: int = Query(10, ge=1, le=100, description="每页数量")
):
    """
    获取所有教练列表（学员端使用）
    """
    try:
        total, coaches_data = await get_all_coaches_for_student(search, page, page_size)
        return {
            "status": 200,
            "message": "success",
            "data": {
                "list": coaches_data,
                "total": total,
                "page": page,
                "page_size": page_size
            }
        }
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving coaches: {str(e)}"}


@coaches.get("/msg/{coach_id}")
async def get_coach_api(coach_id: int):
    """
    根据教练ID获取教练详细信息
    """
    try:
        coach_data = await get_coach_by_id(coach_id)
        if coach_data:
            return {"status": 200, "message": "success", "data": coach_data}
        else:
            return {"status": 404, "message": "Coach not found"}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving coach: {str(e)}"}


@coaches.get("/msg/userId/{user_id}")
async def get_coach_by_user_id_api(user_id: int):
    """
    根据用户ID获取教练信息
    """
    try:
        coach_data = await get_coach_by_user_id(user_id)
        if coach_data:
            return {"status": 200, "message": "success", "data": coach_data}
        else:
            return {"status": 404, "message": "Coach not found"}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving coach: {str(e)}"}


@coaches.post("/add")
async def create_coach_api(data: CoachCreateRequest):
    """
    创建新教练
    """
    try:
        result = await create_coach(data)
        return result
    except Exception as e:
        return {"status": 500, "message": f"Error creating coach: {str(e)}"}


@coaches.put("/update/{coach_id}")
async def update_coach_api(coach_id: int, data: CoachUpdateRequest):
    """
    更新教练信息
    """
    try:
        result = await update_coach(coach_id, data)
        return result
    except Exception as e:
        return {"status": 500, "message": f"Error updating coach: {str(e)}"}


@coaches.delete("/delete/{coach_id}")
async def delete_coach_api(coach_id: int):
    """
    删除教练
    """
    try:
        result = await delete_coach(coach_id)
        return result
    except Exception as e:
        return {"status": 500, "message": f"Error deleting coach: {str(e)}"}
